﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Core.BusinessLogic;
using Module.Core.Common;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.EntitiesInfo;

namespace Web.Cms.Member
{
    public partial class lstChuyenGold : AdminUserControl
    {

        #region Khai báo biến
        private int totalRecords;
        private int page;
        private DateTime dateTo;
        private DateTime dateFrom;
        LogChuyenGoldInfo info = new LogChuyenGoldInfo();
        #endregion

        #region OnInit
        protected override void OnInit(EventArgs e)
        {
            try
            {
                base.OnInit(e);
                if (Request.QueryString["dateFrom"] != null && Request.QueryString["dateFrom"] != "")
                {
                    dateFrom = Convert.ToDateTime(Request.QueryString["dateFrom"].Trim());
                    txtDatefrom.Text = dateFrom.ToString("MM/dd/yyyy");
                }
                else
                {
                    dateFrom = DateTime.Now.AddYears(-2);
                    txtDatefrom.Text = dateFrom.ToString("MM/dd/yyyy");
                }

                if (Request.QueryString["dateTo"] != null && Request.QueryString["dateTo"] != "")
                {
                    dateTo = Convert.ToDateTime(Request.QueryString["dateTo"].Trim());
                    txtDateTo.Text = dateTo.ToString("MM/dd/yyyy");
                }
                else
                {
                    dateTo = DateTime.Now.AddYears(+1);
                    txtDateTo.Text = dateTo.ToString("MM/dd/yyyy");
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        
        #region OnPreRender
        protected override void OnPreRender(EventArgs e)
        {
            try
            {
                page = int.Parse("0" + Request.QueryString["page"]);
                if (page == 0) page = 1;
                base.OnPreRender(e);
                LoadData();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }

        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        #region LoadData
        private void LoadData()
        {

            totalRecords = LogChuyenGoldBL.SelectCount(true);
            lbTotal.Text = "Tổng số bản ghi: " + LogChuyenGoldBL.GetCount(info.LogId, DateTime.Parse(txtDatefrom.Text), DateTime.Parse(txtDateTo.Text));
            DataSet ds;
            ds = LogChuyenGoldBL.ListLogChuyenGoldPage(info.LogId, DateTime.Parse(txtDatefrom.Text), DateTime.Parse(txtDateTo.Text), page, Const.pageSize30, 15, totalRecords, "Default.aspx?ctrlkey=" + CurrentCommand.CtrlKey + "&dateFrom=" + dateFrom + "&dateTo=" + dateTo + "&page");
            if (ds != null)
            {
                DataTable dtbData = ds.Tables[0];
                DataTable dtbPhanTrang = ds.Tables[1];
                if (dtbData.Rows.Count > 0)
                {

                    grdChuyenGold.DataSource = dtbData;
                    grdChuyenGold.RowDataBound += new GridViewRowEventHandler(grdChuyenGold_RowDataBound);
                    grdChuyenGold.DataBind();
                    if (dtbPhanTrang.Rows.Count > 0)
                    {
                        ltlPhanTrang.Text = dtbPhanTrang.Rows[0]["PhanTrang"] + "";


                    }
                }
            }
        }
        #endregion

        protected void grdChuyenGold_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                switch (e.CommandName)
                {
                    case "Commanddelete":
                        try
                        {
                            int logId = Convert.ToInt32(e.CommandArgument);
                            LogChuyenGoldBL.Delete(logId);
                        }
                        catch (Exception ex)
                        {
                            lblMessage.Text = ex.Message;
                        }
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }
        
        protected void grdChuyenGold_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[0].Text = (Const.pageSize30 * (page - 1) + e.Row.DataItemIndex + 1).ToString();
        }

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?ctrlkey=" + CurrentCommand.CtrlKey + "&dateFrom=" + txtDatefrom.Text.Trim() + "&dateTo=" + txtDateTo.Text.Trim() + "&page=" + page);
            //LogChuyenGoldBL.Search(info.LogId, info.LogId, Convert.ToString(txtDateTo.Text));
        }
    }
}